-module(san_gong).
%% @doc 三公游戏逻辑模块，处理游戏逻辑  
-export([check_card_multiple/1,start/0,test/0,check_card_win/1,get_wang/0,get_ply_mode/0,sg_sit_down/2,start_san_gong/1,
		 player_start_xia_zhu/4,xia_zhua_count_down/0,start_qiang_zhuang/3,xtimer_start_next_game/0,xtimer_qiang_zhuang/0,
		 join_room_init_zhuang/0,start_liang_pai/2,next_game_update_staus/0,player_reply_destroy_room/3,
		 destroy_sg_room/1,update_game_staus_to_all/1,jie_suan_cards/1,rand_cards/3,get_sw_sg_card/0,player_offline/2]).
-export([get_stake/1,check_card_da_or_xiao/2]).
-include("common.hrl").
%%倍数  
-define(TIAN_GONG,9).  %%天公
-define(BAO_SAN,9).    %%爆三
-define(SAN_TIAO,5).   %%三条
-define(LEI_GONG,5).   %%雷公
-define(SAN_GONG,4).   %%三公
-define(JIU_DIAN,3).   %%九条
-define(BA_DIAN,2).    %%八点
-define(YI_BEI,1).     %%其余点都是一

%%类型
-define(TIAN_GONG_TYPE,9).  %%天公
-define(BAO_SAN_TYPE,8).    %%爆三
-define(SAN_TIAO_TYPE,7).   %%三条
-define(LEI_GONG_TYPE,6).   %%雷公
-define(SAN_GONG_TYPE,5).   %%三公
-define(DIAN_SHU_TYPE,4).   %%点数类型

-define(MODE_MROOM_OWNER_ZHUANG,1).      %%房主坐庄 
-define(MODE_JIU_DIAN_ZHUANG_TYPE,2).    %%九点上庄
-define(MODE_MIX_WAR,3).                 %%大混�
-define(MODE_SHOW_CARD_QIANG_ZHUANG,4).  %%明牌抢庄
-define(MODE_FREEDOM_QIANG_ZHUANG,5).    %%自由抢庄
-define(MODE_WORLD_WAR,6).               %%世界大战


-define(HAS_ZHUANG_JIA,0).  %%有庄
-define(NO_ZHUANG_JIA,1).   %%没有庄家


%%游戏
-define(PREPARE_OK,1).
-define(STAUS_START_GAME,2).
-define(STAUS_CUO_PAI,3).
-define(STAUS_LIANG_PAI,4).
-define(STAUS_OFFLINE,5).

%%下注
-define(XIA_ZHU_1,1).
-define(XIA_ZHU_2,2).
-define(XIA_ZHU_3,3).
-define(XIA_ZHU_4,4).
-define(XIA_ZHU_5,5).

%%抢庄 
-define(QIANG_ZHUANG,1).
-define(BU_QIANG,0).
%%倒计
-define(COUNTDOWN_QIANG_ZHUANG,8).
-define(COUNTDOWN_XIA_ZHU,10).
-define(COUNTDOWN_NEXT_GAME,15).
-define(COUNTDOWN_UPDATE_STAUS,5).

-define(AGREEN,1).
-define(NO,2).



%%:= 操作表示更新一个已有的 key, => 操作符的意思表示新创建字段 
%% 颜色1 的是小王 ，颜色2 的是大王
%% 1 黑桃  2红桃 3梅花 4方块
get_wang() ->
	[
	 #{color => 1,val => 14},#{color => 2,val => 15}
	].

get_all() ->
	[
	 #{color => 1,val => 1},#{color => 2,val => 1},#{color => 3,val => 1},#{color => 4,val => 1},
	 #{color => 1,val => 2},#{color => 2,val => 2},#{color => 3,val => 2},#{color => 4,val => 2},
	 #{color => 1,val => 3},#{color => 2,val => 3},#{color => 3,val => 3},#{color => 4,val => 3},
	 #{color => 1,val => 4},#{color => 2,val => 4},#{color => 3,val => 4},#{color => 4,val => 4},
	 #{color => 1,val => 5},#{color => 2,val => 5},#{color => 3,val => 5},#{color => 4,val => 5},
	 #{color => 1,val => 6},#{color => 2,val => 6},#{color => 3,val => 6},#{color => 4,val => 6},
	 #{color => 1,val => 7},#{color => 2,val => 7},#{color => 3,val => 7},#{color => 4,val => 7},
	 #{color => 1,val => 8},#{color => 2,val => 8},#{color => 3,val => 8},#{color => 4,val => 8},
	 #{color => 1,val => 9},#{color => 2,val => 9},#{color => 3,val => 9},#{color => 4,val => 9},
	 #{color => 1,val => 10},#{color => 2,val => 10},#{color => 3,val => 10},#{color => 4,val => 10},
	 #{color => 1,val => 11},#{color => 2,val => 11},#{color => 3,val => 11},#{color => 4,val => 11},
	 #{color => 1,val => 12},#{color => 2,val => 12},#{color => 3,val => 12},#{color => 4,val => 12},
	 #{color => 1,val => 13},#{color => 2,val => 13},#{color => 3,val => 13},#{color => 4,val => 13}	
	].

get_sw_sg_card() -> get_all()++get_wang().

test() ->
	xtimer:start_timer(?COUNTDOWN_XIA_ZHU*1000,{?MODULE, xia_zhua_count_down}).

start() ->	
    Plys = fun_room:get_players(),
	L = rand_cards(get_all(),[],Plys),
	{Cardinfo,Scorelist} = jie_suan_cards(L),
	{ok,?GAME_TYPE_NORMAL_SANGONG,Cardinfo,Scorelist}.

rand_cards(_Cards,GetCards,[]) ->GetCards;
rand_cards(Cards,GetCards,[Ply_sid | Next] ) ->
	{Surplus_Cards,Plyscard} = get_rand_player_card(Ply_sid,Cards,[]),
     rand_cards(Surplus_Cards,GetCards++Plyscard,Next).

get_rand_player_card(Ply_sid,Surplus_Cards,Getcards) when length(Getcards) == 3 ->{Surplus_Cards,[{Ply_sid,Getcards}]};
get_rand_player_card(Ply_sid,Surplus_Cards,Getcards) ->
   Index = util:rand(1, erlang:length(Surplus_Cards)),
   Card = lists:nth(Index, Surplus_Cards),
   get_rand_player_card(Ply_sid,Surplus_Cards -- [Card],[Card | Getcards]).

%%随机到牌后结算
jie_suan_cards(List) -> 
	Fun = fun({Sid,Getcards},L) ->
				  case check_card_multiple(Getcards) of
					  {Cardtype,Card_multiple} ->
						  case fun_room:get_player(Sid) of
							  #room_ply{aid =Aid } ->
								  [{Aid,{Cardtype,Card_multiple},Getcards}] ++L;
							  _ -> L
						  end;
					  _ -> L
				  end
		  end,
	ListInfo = lists:foldl(Fun, [], List),
	Scorelist = check_card_win(ListInfo),	
	{ListInfo,Scorelist}.
				  

%% 牌型倍数检查[{Aid,{什么类型牌 , 翻的倍数 }}] {Aid,{Card_Type,Ply_Multiple},Cards}
check_card_multiple([#{color := 1,val := 14},#{color := 2,val := 15},#{val :=Val}]) when Val > 10  ->{?TIAN_GONG_TYPE,?TIAN_GONG};
check_card_multiple([#{val := 3},#{val := 3},#{val := 3}]) ->{?BAO_SAN_TYPE,?BAO_SAN};
check_card_multiple([#{val := Val},#{val := Val},#{val := Val}]) ->{?SAN_TIAO_TYPE,?SAN_TIAO};
check_card_multiple([#{val := Val1},#{val := Val2},#{val := Val3}]) when Val1 == 14 orelse Val1 == 15 andalso Val2 >10 andalso Val3 >10 ->
	{?LEI_GONG_TYPE,?LEI_GONG};
check_card_multiple([#{val := Val1},#{val := Val2},#{val := Val3}]) when Val1 >10 andalso Val2 >10 andalso Val3 >10 ->{?SAN_GONG_TYPE,?SAN_GONG};	
check_card_multiple([#{color:=Color1, val := Val1},#{color:=Color2,val := Val2},#{color:=Color3,val := Val3}]) ->
	case get_card_dian_shu([{Color1,Val1},{Color2,Val2},{Color3,Val3}]) of
		9 ->{?DIAN_SHU_TYPE,?JIU_DIAN};
		8 ->{?DIAN_SHU_TYPE,?BA_DIAN};
		_c ->?log("_c........=~p",[_c]), {?DIAN_SHU_TYPE,?YI_BEI}
	end;																	  
check_card_multiple(_Cards) -> false.

%三公结算6种模式结算
%%房主坐庄 
%%九点上庄
%%大混战
%%明牌抢庄
%%自由抢庄
%%世界大战  
check_card_win(List) ->
	Stake_list = 
		case get(xia_zhu_list) of
			undefiend -> [];
			L -> L
		end,
	case get(zhuang) of
		0 -> no_zhuang_jia(List,List,Stake_list,[]);
		Zhuang_jia when erlang:is_integer(Zhuang_jia) ->
			has_zhuang_jia(Zhuang_jia,List,Stake_list);
		_ -> []
	end.
	

%% 没有庄家 ，每位玩家都要和其他玩家比牌
no_zhuang_jia([],_All_list,_Stake_list,Player_Scorelist) ->Player_Scorelist;
no_zhuang_jia([{Aid,{Card_Type,Ply_Multiple},Cards} | Next ],All_list,Stake_list,Player_Scorelist) ->
	Ply_Stake = 
		case lists:keyfind(Aid, 1, Stake_list) of
			{_,Stake} ->Stake;
			_ ->1 
		end,
	My_lastscore = check_card_to_everyone(Aid,Ply_Stake,Card_Type,Ply_Multiple,Cards,lists:keydelete(Aid, 1, All_list),Stake_list),
	no_zhuang_jia(Next,All_list,Stake_list,Player_Scorelist++My_lastscore).	
  
check_card_to_everyone(Aid,Ply_Stake,Card_Type,Ply_Multiple,Cards,Next,Stake_list) ->
	Fun = fun({Oth_Aid,{Oth_CardType,Oth_Multiple},Oth_Cards},Acc) ->	
				  Oth_stake = get_stake(Oth_Aid,Stake_list),
				  Myscore = 
					  if Oth_CardType > Card_Type ->
							 -1*Ply_Stake*Oth_Multiple;
						 Oth_CardType < Card_Type ->			   
							 Oth_stake*Ply_Multiple;
						 true ->case check_same_card(Cards,Oth_Cards) of
									?ZHUANG_XIAO -> 
										Oth_stake*Ply_Multiple;
									?ZHUANG_DA ->-1*Ply_Stake*Oth_Multiple;
									_ -> 0
								end
					  end,
				  Myscore+Acc
		  end,
	Amount = lists:foldl(Fun, 0, Next),
	[{Aid,Amount}].

get_stake(Aid,Stake_list) ->
	case lists:keyfind(Aid, 1, Stake_list) of
		{_,Stake} ->Stake;
		_ ->1 
	end.
%%获得下注积分
get_stake(Aid) ->
	case get(xia_zhu_list) of
		StakeList when is_list(StakeList) andalso length(StakeList) > 0  ->
			case lists:keyfind(Aid, 1,StakeList) of
				{_,Stake} ->Stake;
				_ ->1 
			end;
		_->1
	end.
	
%%有庄家结算，拿闲家牌一一跟庄家做比较 比庄家大的庄家赔 小的庄家赢
has_zhuang_jia(Zhuang_jia_aid,List,Stake_list) -> 
	case lists:keyfind(Zhuang_jia_aid, 1, List) of
		{_,{Zhuang_card_type,Multiple},Zhuang_cards} ->
			Fun =fun({Aid,{Card_Type,Ply_Multiple},Cards},{L1,Zhuang_score}) ->
						 {Final_score,This_Zhuang_score} = check_card_type(Aid,Card_Type,Ply_Multiple,Cards,Zhuang_card_type,Multiple,Zhuang_cards,Stake_list),
						 {[{Aid,Final_score}] ++ L1,Zhuang_score+This_Zhuang_score}
				 end,
			{Oth_score_list, Zhuang_amount_score} = lists:foldl(Fun, {[],0}, lists:keydelete(Zhuang_jia_aid,1, List)),
			Oth_score_list++[{Zhuang_jia_aid,Zhuang_amount_score}];
		_ -> []
	end.
	
%%先检查三公的牌型大小 比庄家大的直接算庄家陪多少分
check_card_type(Aid,Card_Type,Ply_Multiple,Cards,Zhuang_card_type,Zhuang_Multiple,Zhuang_cards,Stake_list) -> 
	Ply_Stake = 
		case lists:keyfind(Aid, 1, Stake_list) of
			{_,Stake} ->Stake;
			_ ->1 
		end,
	if Zhuang_card_type > Card_Type ->
		   {-1*Ply_Stake*Zhuang_Multiple,Ply_Stake*Zhuang_Multiple};
	   Zhuang_card_type < Card_Type ->
		   {Ply_Stake*Ply_Multiple,-1*Ply_Stake*Ply_Multiple};
	   true ->case check_same_card(Cards,Zhuang_cards) of
				  ?ZHUANG_XIAO -> 
					  {Ply_Stake*Ply_Multiple,-1*Ply_Stake*Ply_Multiple};
				  ?ZHUANG_DA ->{-1*Ply_Stake*Zhuang_Multiple,Ply_Stake*Zhuang_Multiple};
				  _ -> {0,0}
			  end
	end.
		
%%牌型一样则比较最大的牌， 如果最大的牌也一样则比较花色，黑红梅方
check_same_card([#{color:=Color_1,val := Val_1},#{color:=Color_2,val := Val_2},#{color:=Color_3,val := Val_3}],
				[#{color:=Z__Color_1,val :=Z_Val_1},#{color:=Z_Color_2,val :=Z_Val_2},#{color:=Z_Color_3,val :=Z_Val_3}]) ->
	{My_color,Max1} =lists:nth(3, lists:keysort(2, [{Color_1,Val_1},{Color_2,Val_2},{Color_3,Val_3}])),
	{Z_color,Max_Z} = lists:nth(3, lists:keysort(2,[{Z__Color_1,Z_Val_1},{Z_Color_2,Z_Val_2},{Z_Color_3,Z_Val_3}])), 
	check_card_da_or_xiao({My_color,Max1},{Z_color,Max_Z}).

%%两个牌比较大小
check_card_da_or_xiao({Color1,Max1},{Color2,Max2})->
	if Max1 > Max2 -> ?ZHUANG_XIAO;
	   Max1 < Max2 -> ?ZHUANG_DA;
	   true -> 
		   if Color1 > Color2 -> ?ZHUANG_XIAO;
			  true -> ?ZHUANG_DA
		   end
	end.

join_room_init_zhuang()->
	Ply_mode = fun_room:get_room_ply_mode(),
	?log("get(owner)....................=~p",[get(owner)]),
	if Ply_mode == ?MODE_MROOM_OWNER_ZHUANG ->get(owner);
	   true -> case get(zhuang) of
				   ?UNDEFINED -> 0;
				   Zhuang when erlang:is_integer(Zhuang) ->Zhuang;
				   _ -> 0
			   end
	end.

%%三公玩家点坐�这局已经在游戏中的话不能坐下
sg_sit_down(_Sid,Aid) -> 
	case get(room_qp) of
		#room_qp{status = ?ROOM_WAIT,game_times =Game_times } ->
			Need = fun_player:need_card(get(req)),
			case fun_player:check_card(Aid, Need) of
				true ->
					case fun_room:get_ply_by_aid(Aid) of
						Ply = #room_ply{prepare_state = Prepare_state} ->
							?log("Prepare_state.............=~p",[Prepare_state]),
							if Prepare_state ==?PREPARE_OK -> skip;
							   true -> 
								   fun_room:put_player(Ply#room_ply{prepare_state = ?PREPARE_OK}),
								   F2 = fun(PlySid) ->
												Pt = pt_public_class:prepare_list_new(),
												case fun_room:get_player(PlySid) of
													#room_ply{aid = Ply_Aid,prepare_state=Preparestate} ->
														Pt#pt_public_prepare_list{aid=Ply_Aid,prepare_state=Preparestate};
													_ -> #pt_public_prepare_list{}
												end
										end,
								   L = lists:map(F2, fun_room:get_players()),
								   Pt1 = pt_update_sg_prepare_state_d103:new(),								  
								   Pt2 = Pt1#pt_update_sg_prepare_state{prepare_list =L},
								    ?log("L...sit down ...........=~p",[Pt2]),
								   fun_room:send_to_all_player(pt_update_sg_prepare_state_d103:to_binary(Pt2, 0)),
								   check_next_game_start(Game_times)
							%%更新所有人准备状� 告诉房主可以开始游戏了
							end;
						_ -> skip
					end;
				_ -> ?debug("not card")
			end;
		_ -> ?debug("this room no wait staus =~p",[get(room_qp)])
	end.

check_next_game_start(Game_times) when Game_times >= 1 ->
	fun_room:start();
check_next_game_start(_Game_times) -> skip.

%% 开始游�定庄家，  
start_san_gong(Module) ->
	PlayDic = get(room_plys),
	Len = erlang:length(dict:to_list(PlayDic)),
	if Len > 1 -> 
		   Fun = fun({_Sid,#room_ply{prepare_state =Prepare_state}}) ->
						 if Prepare_state =/= ?PREPARE_OK -> true;
							true -> false
						 end
				 end,
		   case lists:filter(Fun, dict:to_list(PlayDic)) of
			   [] ->
				   put(module,Module),
				   gen_server:cast({global, game_center}, {room_start_game,get(id)}),
				   ?log("start_san_gong..............true.."),
				   put_san_gong_zhuang(),
				   start_xtimer(Module),				  
				   fun_room:start_game(),
				   update_game_staus_to_all(?STAUS_START_GAME);
			   _ -> ?debug("has plyer no prepare")
		   end;	   
	   true -> ?debug("usr not enough")
	end.

put_san_gong_zhuang() ->
	Zhuang_Aid = get_ply_mode(),		
	if Zhuang_Aid > 0 ->
		   ?log("Zhuang_Aid..............=~p",[Zhuang_Aid]),
		   update_zhuang_jia(Zhuang_Aid);
	   true -> skip
	end.


%%自由抢庄�明牌抢�开始游戏后十秒倒计时抢�，都没点的�自动随机�，有人点不抢 另外个人也没�自动随�，一个人点不�另个人点�默认另外个是�
start_qiang_zhuang(_Sid,Aid,Qiangzhaung) -> 
	 case fun_room:get_room_ply_mode() of
		  Ply_mode when Ply_mode == ?MODE_SHOW_CARD_QIANG_ZHUANG orelse Ply_mode ==?MODE_FREEDOM_QIANG_ZHUANG ->
			Plys = fun_room:get_players(),
			PlyNum = erlang:length(Plys), 
			case get(qiang_zhuang_list) of
				undefined ->put(qiang_zhuang_list,[{Aid,Qiangzhaung}]);
				L when length(L) > 0 ->
					case lists:keyfind(Aid, 1, L) of
						{_,_} -> skip;
						_ ->put(qiang_zhuang_list,[{Aid,Qiangzhaung}]++L),
							if length(L)+1 == PlyNum ->
								   qiang_zhuang_mode_rand(get(qiang_zhuang_list));
							   true ->  skip
							end
					end
			end;			
		_ -> skip
	end.


qiang_zhuang_mode_rand(Qiang_zhuang_list) when length(Qiang_zhuang_list) > 0  ->
	case lists:filter(fun({_Aid,Qiang_state}) -> if Qiang_state == ?QIANG_ZHUANG -> true;
												   true -> false
												end end, Qiang_zhuang_list) of
		[] -> Rand = util:rand(1, length(Qiang_zhuang_list)),
			  {Zhuang_Aid,_} = lists:nth(Rand, Qiang_zhuang_list),
			  put(zhuang,Zhuang_Aid),
			  update_zhuang_jia(Zhuang_Aid);
			  %%更新给客户端 那个是庄
        L when length(L) > 0 ->
			Rand = util:rand(1, length(L)),
			{Zhuang_Aid,_} = lists:nth(Rand, L),
			put(zhuang,Zhuang_Aid),
			update_zhuang_jia(Zhuang_Aid)
			  %%更新给客户端 那个是庄
    end.

xtimer_qiang_zhuang() ->
	Plys = fun_room:get_players(),
	case get(qiang_zhuang_list) of
		undefined ->
			Rand_sid = util:rand(1, length(Plys)),
			case fun_room:get_player(Rand_sid) of
				#room_ply{aid = Zhuang_Aid} ->
					put(zhuang,Zhuang_Aid),
					update_zhuang_jia(Zhuang_Aid);
				_ -> skip
			end;
		Qiang_zhuang_list when length(Qiang_zhuang_list) > 0 -> 
			case lists:filter(fun({_Aid,Qiang_state}) -> if Qiang_state == ?QIANG_ZHUANG-> true;
															true -> false
														 end end, Qiang_zhuang_list) of
				[] -> Rand_sid = util:rand(1, length(Plys)),
					  case fun_room:get_player(Rand_sid) of
						  #room_ply{aid = Zhuang_Aid} ->put(zhuang,Zhuang_Aid),
														update_zhuang_jia(Zhuang_Aid);
						  _ -> skip
					  end;
				%%更新给客户端 那个是庄
				L when length(L) > 0 ->
					Rand = util:rand(1, length(L)),
					{Zhuang_Aid,_} = lists:nth(Rand, L),
					put(zhuang,Zhuang_Aid),
					update_zhuang_jia(Zhuang_Aid)
			%%更新给客户端 那个是庄
			end
	end.

update_zhuang_jia(Zhuang_Aid) -> 
	Pt =pt_update_sg_zhuang_d104:new(),
	Pt1 =Pt#pt_update_sg_zhuang{zhuang_aid=Zhuang_Aid},
	fun_room:send_to_all_player(pt_update_sg_zhuang_d104:to_binary(Pt1, 0)).

%%开始游戏后根据不同模式加定时间 
%% 1 房主坐庄 5下注秒倒计时， 倒计时结束后没有下注的玩家默认下
%% 2 
start_xtimer(Module) ->
	if Module == ?MODE_MROOM_OWNER_ZHUANG orelse Module ==?MODE_JIU_DIAN_ZHUANG_TYPE orelse Module ==?MODE_MIX_WAR
	   orelse Module == ?MODE_WORLD_WAR -> 
		   xtimer:start_timer(?COUNTDOWN_XIA_ZHU*1000,{?MODULE, xia_zhua_count_down});
	   Module == ?MODE_FREEDOM_QIANG_ZHUANG -> 
		   xtimer:start_timer(?COUNTDOWN_QIANG_ZHUANG*1000,{?MODULE, xtimer_qiang_zhuang}),
		   xtimer:start_timer((?COUNTDOWN_QIANG_ZHUANG+?COUNTDOWN_XIA_ZHU)*1000,{?MODULE, xia_zhua_count_down});
	   true -> skip
	end.

%%玩家开始下，所有人下注完成后开始发
% 全部下注后开始加定时15秒后自动亮牌  
player_start_xia_zhu(_Sid,Aid,Xiazhu_score,Zhuang_Aid) when Aid =/= Zhuang_Aid  ->
	New_xiazhu_score =check_xia_zhu_astrict(Aid,Xiazhu_score),
	Plys = fun_room:get_players(),
	Molde = fun_room:get_room_module(),  
	PlyNum = erlang:length(Plys), 
	?log("player_start_xia_zhu...............=~p,PlyNum=~p",[Xiazhu_score,PlyNum]),
	case get(xia_zhu_list) of
		undefined ->put(xia_zhu_list,[{Aid,Xiazhu_score},{Zhuang_Aid,1}]),
					update_xia_zhu_to_all([{Aid,Xiazhu_score}]),
					if PlyNum == 2 ->
						   case Molde:start() of
							   {ok,?GAME_TYPE_NORMAL_SANGONG,Cards,Scorelist} ->
								   san_gong_start_game(Cards,Scorelist);
							   {ok,?GAME_TYPE_JINHUA,SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList}->
								   san_gong_start_game(SanGongInfo,SanGongScoreList++JinHuaSanGongScoreList),
								   jinhua_san_gong_start_game(JinHuaSanGongInfo, JinHuaSanGongScoreList);
							   {ok,?GAME_TYPE_SHUANGWANG_SANGONG,Cards,Scorelist} ->
								   san_gong_start_game(Cards,Scorelist);
							   _ -> ?debug("start false")
						   end;
					   true -> skip
					end;
		L when length(L) > 0 ->
			case lists:keyfind(Aid, 1, L) of
				{_,_} ->?log("skip......."), skip;
				_ ->put(xia_zhu_list,[{Aid,Xiazhu_score}]++L),
					update_xia_zhu_to_all([{Aid,Xiazhu_score}]),
					if length(L)+1 == PlyNum ->
						   case Molde:start() of
							   {ok,?GAME_TYPE_NORMAL_SANGONG,Cards,Scorelist} ->
								   san_gong_start_game(Cards,Scorelist);
							   {ok,?GAME_TYPE_JINHUA,SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList}->
								   san_gong_start_game(SanGongInfo,SanGongScoreList++JinHuaSanGongScoreList),
								   jinhua_san_gong_start_game(JinHuaSanGongInfo, JinHuaSanGongScoreList);
							   {ok,?GAME_TYPE_SHUANGWANG_SANGONG,Cards,Scorelist} ->
								   san_gong_start_game(Cards,Scorelist);
							   _ -> ?debug("start false")
						   end;
					   true -> skip
					end
			end
	end;
player_start_xia_zhu(_Sid,_Aid,_Xiazhu_score,_Zhuang_Aid) -> skip.

%%下注限制 检查
check_xia_zhu_astrict(Aid,Xiazhu_score) ->
	case fun_room:get_room_advanced_option() of
		[Option1,_] ->
			if Option1 == ?OK_TICK ->
				   case get(xia_zhu_list) of
					   undefined ->Xiazhu_score;
					   L when length(L) > 0 ->
						   case lists:keyfind(Aid, 1, L) of
							   {_,Stake} ->if Xiazhu_score >= Stake -> Xiazhu_score;
											  true ->false
										   end;
							   _ ->Xiazhu_score
						   end
				   end;
			   true -> Xiazhu_score
			end;
		_ -> false
	end.

				   
	
	
%%下注倒计10秒后如果有玩家没有下注默
xia_zhua_count_down() -> 
	?log("xia_zhua_count_down................."),
	Plys = fun_room:get_players_aid_list(),
	PlyNum = erlang:length(Plys), 
	Molde = fun_room:get_room_module(),
	case get(xia_zhu_list) of
		undefined -> L = lists:map(fun(PlyAid) -> {PlyAid,?XIA_ZHU_1} end,Plys),
					 put(xia_zhu_list,L),
					 update_xia_zhu_to_all(L),			
					 if length(L) == PlyNum ->
							case Molde:start() of
								{ok,?GAME_TYPE_NORMAL_SANGONG,Cards,Scorelist} ->
									san_gong_start_game(Cards,Scorelist);
								{ok,?GAME_TYPE_JINHUA,SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList}->
									san_gong_start_game(SanGongInfo,SanGongScoreList++JinHuaSanGongScoreList),
									jinhua_san_gong_start_game(JinHuaSanGongInfo, JinHuaSanGongScoreList);
								{ok,?GAME_TYPE_SHUANGWANG_SANGONG,Cards,Scorelist} ->
									san_gong_start_game(Cards,Scorelist);
								_ -> ?debug("start false")
							end;
						true -> skip
					 end;
		L when length(L) > 0 ->		
			if length(L) == PlyNum ->?log("xia zhu ok skip............."), skip;
			   true ->
				   Fun = fun(PlyAid) ->
								 case lists:keyfind(PlyAid, 1, L) of
									 {_,_} -> true;
									 _ -> false
								 end
						 end,
				   {_Haslist,Notlist} = lists:partition(Fun, Plys),
				   L1 = lists:map(fun(Ply_Aid) -> {Ply_Aid,?XIA_ZHU_1} end, Notlist),
				   put(xia_zhu_list,L++L1),	
				   update_xia_zhu_to_all(L1),
				   case Molde:start() of
					   {ok,?GAME_TYPE_NORMAL_SANGONG,Cards,Scorelist} ->
						   san_gong_start_game(Cards,Scorelist);
					   {ok,?GAME_TYPE_JINHUA,SanGongInfo,JinHuaSanGongInfo,SanGongScoreList,JinHuaSanGongScoreList}->
						   san_gong_start_game(SanGongInfo,SanGongScoreList++JinHuaSanGongScoreList),
						   jinhua_san_gong_start_game(JinHuaSanGongInfo, JinHuaSanGongScoreList);
					   {ok,?GAME_TYPE_SHUANGWANG_SANGONG,Cards,Scorelist} ->
						   san_gong_start_game(Cards,Scorelist);
					   _ -> ?debug("start false")
				   end
			end
	end.

%% get_xia_zhu_val(Aid) -> 
%% 	case get(xia_zhu_list) of
%% 		undefined -> 1;
%% 		L when length(L) > 0 ->
%% 			case lists:keyfind(Aid, 1, L) of
%% 				{_,Val} -> Val;
%% 				_ -> 1
%% 			end
%% 	end.
	
update_xia_zhu_to_all(Xiazhu_list)->
	Pt =pt_update_sg_xiazhu_score_d105:new(),
	F = fun({Aid,Xiazhu_score}) ->
				Pt1 =pt_public_class:xiazhu_list_new(),
				Pt1#pt_public_xiazhu_list{aid=Aid,score=Xiazhu_score}
		end,
	Pt1 =Pt#pt_update_sg_xiazhu_score{xiazhu_score=lists:map(F, Xiazhu_list)},
	?log("Pt1.......update_xia_zhu_to_all.............=~p",[Pt1]),
	fun_room:send_to_all_player(pt_update_sg_xiazhu_score_d105:to_binary(Pt1, 0)). 
	
san_gong_start_game(Cards,Scorelist) ->
	?log("san_gong_start_game..............=~p",[{Cards,Scorelist}]),
	Qp=get(room_qp),
	put(room_qp,Qp#room_qp{status = ?ROOM_GAME,game_times = Qp#room_qp.game_times + 1,zhuang_index = get(zhuang),
						   fighting_record=get_fighting_record(Qp#room_qp.game_times + 1,Cards,Scorelist,Qp#room_qp.fighting_record),jie_suan_list=Scorelist}),
	send_card_to_all(Cards),
	update_game_staus_to_all(?STAUS_CUO_PAI),
	check_last_jiu_dian_player(Cards),
	xtimer_finish().

jinhua_san_gong_start_game(JinHuaSanGongInfo,JinHuaSanGongScoreList) ->
	?log("jinhua_san_gong_start_game............=~p",[{JinHuaSanGongInfo,JinHuaSanGongScoreList}]),

	Fun = fun({Aid,Score},Acc)->
				  case  lists:keyfind(Aid, 1, JinHuaSanGongInfo) of
					  {Aid,JinHuaSanGongMultiole,JinHuaSanGongMultiole,Getcards}->
						  Acc ++ [{Aid,JinHuaSanGongMultiole,Score}];
					  _->
						  Acc
				  end
		  end,
	JinHuaInfoList = lists:foldl(Fun, [], JinHuaSanGongScoreList),
	Pt = pt_jinhua_card_info_d203:new(),
	Fun1 = fun({Aid,JinHuaSanGongMultiole,Score})->
				   Ptm = pt_public_class:jinhua_card_info_new(),
				   Ptm#pt_public_jinhua_card_info{aid=Aid,score=Score,card_type=JinHuaSanGongMultiole}
		   end,
	Data = lists:map(Fun1, JinHuaInfoList),
	Pt1 = Pt#pt_jinhua_card_info{jinhua_card_info = Data},
	fun_room:send_to_all_player(pt_jinhua_card_info_d203:to_binary(Pt1)).

%%  zhuang�[{aid,[{}],几点�牌型�几倍，下注的分数}]
send_card_to_all(CardsInfo) ->
	Fun = fun({Aid,{Card_Type,Ply_Multiple},Cards}) ->
				  Cardlist = get_card_list(Cards),
				  Pt = pt_public_class:sg_card_info_new(),
				  F = fun({Color,Val}) -> 
							  Pt1 = pt_public_class:sg_card_list_new(),
							  Pt1#pt_public_sg_card_list{color=Color,val=Val}
					  end,					  
				  Pt#pt_public_sg_card_info{aid=Aid,card_list=lists:map(F, Cardlist),card_type=Card_Type,card_multiple=Ply_Multiple,
											card_dian_shu = get_card_dian_shu(Cardlist)}
		  end,
	CardInfo = lists:map(Fun, CardsInfo),
	Pt2 = pt_sg_card_info_d102:new(),
	Pt3 = Pt2#pt_sg_card_info{card_info=CardInfo},
	?log("send_card_to_all.................=~p",[Pt3]),
	fun_room:send_to_all_player(pt_sg_card_info_d102:to_binary(Pt3, 0)).

send_jinhua_card_to_all(CardsInfo) ->
	Fun = fun({Sort,Aid,Card_Type,Ply_Multiple,Cards}) ->
				  Cardlist = get_card_list(Cards),
				  Pt = pt_public_class:sg_card_info_new(),
				  F = fun({Color,Val}) -> 
							  Pt1 = pt_public_class:sg_card_list_new(),
							  Pt1#pt_public_sg_card_list{color=Color,val=Val}
					  end,					  
				  Pt#pt_public_sg_card_info{aid=Aid,card_list=lists:map(F, Cardlist),card_type=Card_Type,card_multiple=Ply_Multiple,
											card_dian_shu = get_card_dian_shu(Cardlist)}
		  end,
	CardInfo = lists:map(Fun, CardsInfo),
	Pt2 = pt_sg_card_info_d102:new(),
	Pt3 = Pt2#pt_sg_card_info{card_info=CardInfo},
	?log("send_card_to_all.................=~p",[Pt3]),
	fun_room:send_to_all_player(pt_sg_card_info_d102:to_binary(Pt3, 0)).

get_card_list([#{color:=Color_1,val := Val_1},#{color:=Color_2,val := Val_2},#{color:=Color_3,val := Val_3}]) ->
	[{Color_1,Val_1},{Color_2,Val_2},{Color_3,Val_3}];
get_card_list(_) -> [].

get_fighting_record(Gametimes,Cards,Scorelist,Old_list) ->
	ZhuangAid = get(zhuang),
	Fun = fun({Aid,Curscore}) ->
				  Ply_Name =
					  case fun_room:get_ply_by_aid(Aid) of
						  #room_ply{name =Name} ->Name;
						  _ -> ""
					  end,
				  Stake =get_stake(Aid),
				  Cardlist =get_card(Aid,Cards),
				  Dian_shu = get_card_dian_shu(Cardlist),
				  Zhuangstate = check_zhuang(ZhuangAid,Aid),
				  {Aid,Ply_Name,Curscore,Stake,Cardlist,Dian_shu,Zhuangstate}
		  
		  end,
	L = lists:map(Fun, Scorelist),
	[{Gametimes,L}]++Old_list.
		   
get_card(Aid,Cardlist) ->
	case lists:keyfind(Aid, 1, Cardlist) of
		{Aid,_,Cards} ->get_card_list(Cards);
		_ -> []
	end.
check_zhuang(ZhuangAid,Aid) when ZhuangAid == Aid ->1;
check_zhuang(_ZhuangAid,_Aid) -> 0.

get_card_dian_shu(Cards) ->
	case lists:filter(fun({_,Val}) -> if Val < 10 -> true; true -> false end end,Cards) of
		[] -> 0;
		L when length(L) > 0 ->
			Sum = lists:sum(lists:map(fun({_,V}) ->V end, L)),
			if Sum >10 -> case Sum/10 of
							  Num when erlang:is_integer(Num) ->0;
							  _ -> Sum rem 10
						  end;
			   true -> Sum
			end
	end.

start_liang_pai(Sid,Aid) ->
	?log("start_liang_pai..................=~p",[get(liang_pai_list)]),
	case fun_room:get_player(Sid) of
		Ply = #room_ply{prepare_state =?STAUS_CUO_PAI} ->
			Plys = fun_room:get_players(),
			PlyNum = erlang:length(Plys), 
			case get(liang_pai_list) of
				?UNDEFINED -> 
					?log("Aid..........................=~p",[Aid]),
					put(liang_pai_list,[Aid]),
					fun_room:put_player(Ply#room_ply{prepare_state = ?STAUS_LIANG_PAI}),
					send_player_staus_to_all([Aid],?STAUS_LIANG_PAI);
				L when length(L) > 0 ->
					case lists:member(Aid, L) of
						true ->?log("skip....."), skip;
						_ ->put(liang_pai_list,[Aid]++L),
							fun_room:put_player(Ply#room_ply{prepare_state = ?STAUS_LIANG_PAI}),
							send_player_staus_to_all([Aid]++L,?STAUS_LIANG_PAI),								
							if length(L)+1 == PlyNum ->		
								    ?log("start_next_game.................................=~p",[{length(L)+1,PlyNum}]),
								   start_next_game();							
							   true -> skip
							end
					end
			end;
		_ ->?log("skip.....=~p",[get(liang_pai_list)]),skip
	end.

send_player_staus_to_all(Usrlist,Staus) ->	
	Pt =pt_update_sg_prepare_state_d103:new(),
	F = fun(Aid) ->
				Pt1 = pt_public_class:prepare_list_new(),
				Pt1#pt_public_prepare_list{aid=Aid,prepare_state=Staus}
		end,
	Pt2 = Pt#pt_update_sg_prepare_state{prepare_list =lists:map(F, Usrlist)},
	?log("send_liang_pai_to_all ..............= ~p",[Pt2]),
	fun_room:send_to_all_player(pt_update_sg_prepare_state_d103:to_binary(Pt2, 0)).

%%所有人都亮牌后开始下一�发送准备状态为0 
start_next_game()->
	Max_times = fun_room:get_room_max_times(),
	case get(room_qp) of
		Qp =#room_qp{game_times =Game_times,fighting_record=Fighting_record,jie_suan_list=Jiesuan_score_list}->				
			update_player_score(Jiesuan_score_list),
			send_jie_suan_to_all(Jiesuan_score_list,Game_times),			
			case get(finish_timer) of
				?UNDEFINED ->?log("cancel_timer....skip............"),skip;
				Auto_finish_xtimer ->
					?log("cancel_timer................"),
					xtimer:cancel_timer(Auto_finish_xtimer)			
			end,	
			clear_last_room_dict(),
			put(room_qp,Qp#room_qp{status = ?ROOM_WAIT}),		
			if Game_times+1 =< Max_times ->				  
				   xtimer:start_timer(10*1000,{?MODULE, next_game_update_staus});	
			   Game_times == 10 ->
				   gen_server:cast({global,game_center},{update_sg_fighting_record,get(zhuang),get(id),fun_room:get_room_type(),fun_room:get_room_ply_mode(),
														 fun_room:get_room_score(),Game_times,Fighting_record,get_jiesuan_score_list()});
			   true ->?debug("dont > max room times")
			end;
		_ -> skip
	end.

get_jiesuan_score_list() ->
	Fun = fun(Ply_sid) ->
				  case fun_room:get_player(Ply_sid) of
					  #room_ply{aid =Ply_Aid,score = Amount_score} -> 				
						  {Ply_Aid,Amount_score};
					  _ ->{0,0}
				  end
		  end,
	lists:map(Fun, fun_room:get_players()).

send_jie_suan_to_all(Jiesuan_score_list,Game_times) ->
	Pt = pt_update_sg_jie_suan_score_d106:new(),
	F1 = fun({Ply_Aid,Add_score},Acc) ->
				 Pt1 = pt_public_class:score_list_new(),
				 case fun_room:get_ply_by_aid(Ply_Aid) of
					 #room_ply{score = Amount_score} -> 	
						 case lists:keyfind(Ply_Aid, #pt_public_score_list.aid, Acc) of
							 #pt_public_score_list{}->
								 Acc;
							 _->
						 		Acc ++ [Pt1#pt_public_score_list{aid=Ply_Aid,score=Add_score,amount_score=Amount_score}]
						 end;
					 _ ->?log("2222222222"),Acc
				 end
		 end,
	Pt2 = Pt#pt_update_sg_jie_suan_score{score_list=lists:foldl(F1,[], Jiesuan_score_list),game_times=Game_times},
	?log("Pt2........send_jie_suan_to_all................=~p",[Pt2]),
	fun_room:send_to_all_player(pt_update_sg_jie_suan_score_d106:to_binary(Pt2, 0)).

next_game_update_staus() ->
	update_game_staus_to_all(0).

get_ply_mode() ->
	case get(room_qp) of
		#room_qp{game_times  = Game_times} -> 
			Ply_mode = fun_room:get_room_ply_mode(),
			if Ply_mode == ?MODE_MROOM_OWNER_ZHUANG ->put(zhuang,get(owner)),get(owner);
			   Ply_mode == ?MODE_JIU_DIAN_ZHUANG_TYPE -> 
				   if Game_times == 0 ->
						  Rand_sid = util:rand(1, fun_room:get_players()),
						  case fun_room:get_player(Rand_sid) of
							  #room_ply{aid=Aid} ->put(zhuang,Aid),Aid;
							  _ -> 0
						  end;
					  true -> skip
				   end;
			   Ply_mode == ?MODE_MIX_WAR ->
				   put(zhuang,0), 0;
			   Ply_mode == ?MODE_WORLD_WAR ->
				   put(zhuang,0),
				   init_world_war_xiazhu(),
				   0;				   
			   true ->0
			end;
		_ -> 0
	end.

%%世界大战默认没有庄家 ，下注分数默��
init_world_war_xiazhu() -> xia_zhua_count_down().

%%九点模式检查上一轮是否有9点的玩家
check_last_jiu_dian_player(CardInfo) ->
	case fun_room:get_room_ply_mode() of
		?MODE_JIU_DIAN_ZHUANG_TYPE ->	  
			Fun = fun({_Aid,{_,_},Cards}) ->
						  Cardlist = get_card_list(Cards), 
						  Dian_shu = get_card_dian_shu(Cardlist),
						  if Dian_shu == 9 -> true;
							 true -> false
						  end
				  end, 
			case lists:filter(Fun, CardInfo) of
				[] -> skip;
				[{New_Aid,_,_}]  ->put(zhuang,New_Aid);
				L when length(L) > 1 ->
					find_jiudian_player([{Aid,Cards}||{Aid,_,Cards} <- L])
			end;
		_ -> skip
	end.

find_jiudian_player([{Zhuang_Aid,_}]) -> put(zhuang,Zhuang_Aid);
find_jiudian_player([{Aid,Cards} | Next]) ->
	{Next_Aid,Nextcard} =  lists:nth(1, Next),
	case check_same_card(Cards,Nextcard) of
		?ZHUANG_DA ->find_jiudian_player([{Next_Aid,Nextcard}]++lists:keydelete(Next_Aid, 1, Next));
		?ZHUANG_XIAO ->find_jiudian_player([{Aid,Cards}]++lists:keydelete(Next_Aid, 1, Next));
		_ -> skip
	end.
			
	

%%更新给所有人在搓牌状�
update_game_staus_to_all(Staus) ->
	F2 = fun(PlySid) ->								
				 Pt = pt_public_class:prepare_list_new(),
				 case fun_room:get_player(PlySid) of
					 Ply = #room_ply{aid = Aid} ->
						 fun_room:put_player(Ply#room_ply{prepare_state = Staus}),
						 Pt#pt_public_prepare_list{aid=Aid,prepare_state=Staus};
					 _ -> #pt_public_prepare_list{}
				 end
		 end,
	L = lists:map(F2, fun_room:get_players()),
	Pt1 = pt_update_sg_prepare_state_d103:new(),
	Pt2 = Pt1#pt_update_sg_prepare_state{prepare_list =L},
	?log("update_game_staus_to_all ..............= ~p",[L]),
	fun_room:send_to_all_player(pt_update_sg_prepare_state_d103:to_binary(Pt2, 0)).

%%这局打完  15秒后开始下一局  ，所有人准备状态改为不准备 �根据模式重新选庄
xtimer_finish() -> 
	Xtimer = xtimer:start_timer(?COUNTDOWN_NEXT_GAME*1000,{?MODULE, xtimer_start_next_game}),
	?log("Xtimer..............=~p",[Xtimer]),
	put(finish_timer,Xtimer).

xtimer_start_next_game() ->
	?log("1111111111111"),
	Max_times = fun_room:get_room_max_times(),
	case get(room_qp) of
		Qp =#room_qp{game_times =Game_times,fighting_record =Fighting_record,jie_suan_list=Jiesuan_score_list} ->		
			put(room_qp,Qp#room_qp{status = ?ROOM_WAIT}),		
			check_all_usr_liang_pai(),
			update_player_score(Jiesuan_score_list),
			send_jie_suan_to_all(Jiesuan_score_list,Game_times),				
			?log("xtimer_start_next_game.................."),
			if Game_times+1 =< Max_times -> 
				    xtimer:start_timer(?COUNTDOWN_UPDATE_STAUS*1000,{?MODULE, update_game_staus_to_all,0});
			   Game_times ==10 ->
				   gen_server:cast({global,game_center},{update_sg_fighting_record,get(owner),get(zhuang),get(id),fun_room:get_room_type(),fun_room:get_room_ply_mode(),
														 fun_room:get_room_score(),Game_times,Fighting_record,get_jiesuan_score_list()});
			   true-> skip
			end,
			clear_last_room_dict();
		_ -> skip
	end.

check_all_usr_liang_pai() ->
	Plys = fun_room:get_players(),
	PlyNum = erlang:length(Plys), 
	case get(liang_pai_list) of
		?UNDEFINED -> 
			update_game_staus_to_all(?STAUS_LIANG_PAI);
		L when length(L) > 0 ->
			if length(L) == PlyNum ->skip;
			   true ->						 
				   Fun = fun(Plysid) -> 
								 case fun_room:get_player(Plysid) of
									 #room_ply{prepare_state =Prepare_state } ->
										 if Prepare_state =/= ?STAUS_LIANG_PAI -> true;
											true -> false
										 end;
									 _ -> false
								 end
						 end,
				   case lists:filter(Fun, fun_room:get_players()) of
					   [] -> skip;
					   Notlist when length(Notlist) > 0 ->
						   send_player_staus_to_all(lists:map(fun(Playersid) ->
																   case fun_room:get_player(Playersid) of
																	   #room_ply{aid =Aid} -> Aid;
																	   _ -> 0
																   end 
														   end, Notlist),?STAUS_LIANG_PAI)
				   end
			end
	end.

%% 根据模式来是否清除上一局的庄家， �玩家下注情�
clear_last_room_dict() ->
	case get(qiang_zhuang_list) of
		undefined -> skip;
		_ -> erlang:erase(qiang_zhuang_list)
	end,	
	erlang:erase(finish_timer),
	erlang:erase(liang_pai_list),
	?log("erase.................=~p",[get(liang_pai_list)]).


%%更新玩家分数
update_player_score(Scorelist) ->
	Fun = fun({Aid,Score}) ->
				  case fun_room:get_ply_by_aid(Aid) of
					  Roomply = #room_ply{score=Old_score} ->
						  fun_room:put_player(Roomply#room_ply{score=Old_score+Score});
					  _ -> skip
				  end 
		  end,
	lists:foreach(Fun,Scorelist).

player_reply_destroy_room(_Sid,_Aid,?NO) ->
	erlang:erase(reply_destroy_room),
	Ptg = pt_action_d001:new(),
	Ptg1 = Ptg#pt_action{action = ?ACTION_SG_REPLY_NO},
	?log("destroy_room...............Ptg1...=~p",[Ptg1]),
	fun_room:send_to_all_player(pt_action_d001:to_binary(Ptg1));

player_reply_destroy_room(Sid,Aid,?AGREEN) ->
	Plys = fun_room:get_players(),
	PlyNum = erlang:length(Plys), 
	?log("agren......................"),
	Ptg = pt_action_d001:new(),
	Ptg1 = Ptg#pt_action{action = ?ACTION_SG_REPLY_AGREEN},
	?log("destroy_room...............Ptg1...=~p",[Ptg1]),
	?send(Sid,pt_action_d001:to_binary(Ptg1)),
	case get(reply_destroy_room) of
		?UNDEFINED -> 
			if PlyNum-1 == 1 ->
				   destroy_sg_room(?ACTION_SG_DESTROY_OK);
			   true -> put(reply_destroy_room,[Aid])
			end;			   
		L when length(L) > 0 ->
			if length(L)+1 == PlyNum-1 ->destroy_sg_room(?ACTION_SG_DESTROY_OK);
			   true ->put(reply_destroy_room,[Aid]++L)
			end
	end;	
player_reply_destroy_room(_Sid,_Aid,Data) -> ?debug("reply error Data =~p",[Data]),error. 

destroy_sg_room(Action) ->
	case get(room_qp) of
		#room_qp{game_times =Game_times,fighting_record=Fighting_record}->			
			gen_server:cast({global,game_center},{update_sg_fighting_record,get(zhuang),get(id),fun_room:get_room_type(),fun_room:get_room_ply_mode(),
												  fun_room:get_room_score(),Game_times,Fighting_record,get_jiesuan_score_list()}),
			Fun = fun(PlySid) ->
						  case fun_room:get_player(PlySid) of
							  #room_ply{sid = PlySid,aid =PlyAid} ->
								  Ptg = pt_action_d001:new(),
								  Ptg1 = Ptg#pt_action{action = Action},
								  ?log("destroy_room.ok..............Ptg1...=~p,PlySid=~p",[Ptg1,PlySid]),
								  ?send(PlySid,pt_action_d001:to_binary(Ptg1)),
								  gen_server:cast(PlySid,{set_val, PlyAid, 0, 1}),
								  gen_server:cast({global,game_center},{leave_room,PlySid,PlyAid});
							  _ -> skip
						  end
				  end,
			lists:foreach(Fun, fun_room:get_players()),
			fun_room:stop_room();
		_ -> skip
	end.

player_offline(Sid,Aid) ->
	case fun_room:get_player(Sid) of
		Ply=#room_ply{} ->
			fun_room:put_player(Ply#room_ply{prepare_state = ?STAUS_OFFLINE}),
			send_player_staus_to_all([Aid],?STAUS_OFFLINE);
		_ -> skip
	end.
	   

